package com.sunl.mall.ali.mq.springboot;

import com.sunl.mall.ali.mq.springboot.config.ServiceConfig;
import com.sunl.mall.ali.mq.springboot.producer.CommonProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.Arrays;

@SpringBootApplication
public class Application {
    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(ServiceConfig.class);
        CommonProducer producer = ctx.getBean(CommonProducer.class);
        System.out.println("OK");
        for (int i = 0; i < 5; i++) {
           // producer.sendCommonMq();
        }
        printDebugInfo(ctx);
    }

    private static void printDebugInfo(ConfigurableApplicationContext ctx) {
        String debugProperty = "debug";
        String trueValue = "true";
        if (trueValue.equalsIgnoreCase(ctx.getEnvironment().getProperty(debugProperty))) {
            StringBuilder allBeanNameStr = new StringBuilder(500);
            allBeanNameStr.append("----------------- spring beans ------------------------\n");
            String[] beanNames = ctx.getBeanDefinitionNames();
            allBeanNameStr.append("beanNames 总数：" + beanNames.length + "\n");
            Arrays.sort(beanNames);
            for (String bn : beanNames) {
                allBeanNameStr.append(bn + "\n");
            }
            allBeanNameStr.append("------------------ end -----------------------");
            log.info(allBeanNameStr.toString());

        }
        String successInfo = String.format("########[微服务启动成功]###########");
        log.info(successInfo);
    }
}
